# coding: utf-8
from typing import Union
from pathlib import Path
import io
import requests
from PySide6.QtCore import QUrl
from PySide6.QtGui import QImage
from PIL import Image


class RequestBase:
    headers = {}
    base_url = ''

    @classmethod
    def send(cls, method: str, url: str, *args, **kwargs) -> requests.Response:
        """
        使用给定的方法和参数向指定的URL发送请求。
        :param url:
        :param args:
        :param method:
        :param kwargs:
        :return:
        """
        response = requests.request(method, url, *args, **kwargs)
        response.raise_for_status()
        response.encoding = 'utf-8'
        return response

    @classmethod
    def get(cls, url, *args, **kwargs):
        """
        使用GET方法向指定的URL发送请求。
        :param url:
        :param args:
        :param kwargs:
        :return:
        """
        return cls.send('GET', url, *args, **kwargs)

    @classmethod
    def post(cls, url, *args, **kwargs):
        """
        使用POST方法向指定的URL发送请求。
        :param url:
        :param args:
        :param kwargs:
        :return:
        """
        return cls.send('POST', url, *args, **kwargs)

    @classmethod
    def put(cls, url, *args, **kwargs):
        """
        使用PUT方法向指定的URL发送请求。
        :param url:
        :param args:
        :param kwargs:
        :return:
        """
        return cls.send('PUT', url, *args, **kwargs)

    @classmethod
    def delete(cls, url, *args, **kwargs):
        """
        使用DELETE方法向指定的URL发送请求。
        :param url:
        :param args:
        :param kwargs:
        :return:
        """
        return cls.send('DELETE', url, *args, **kwargs)

    @classmethod
    def head(cls, url, *args, **kwargs):
        """
        使用HEAD方法向指定的URL发送请求。
        :param url:
        :param args:
        :param kwargs:
        :return:
        """
        return cls.send('HEAD', url, *args, **kwargs)

    @classmethod
    def options(cls, url, *args, **kwargs):
        """
        使用OPTIONS方法向指定的URL发送请求。
        :param url:
        :param args:
        :param kwargs:
        :return:
        """
        return cls.send('OPTIONS', url, *args, **kwargs)

    @classmethod
    def patch(cls, url, *args, **kwargs):
        """
        使用PATCH方法向指定的URL发送请求。
        :param url:
        :param args:
        :param kwargs:
        :return:
        """
        return cls.send('PATCH', url, *args, **kwargs)


def download_image(url: Union[str, QUrl], save_file_path: str, *args, **kwargs):
    """
    下载图片到本地
    :param url: 图片的URL
    :param save_file_path: 保存图片的路径
    :return:
    """
    if isinstance(url, QUrl):
        url = url.toString()
    response = requests.get(url, *args, **kwargs)
    response.raise_for_status()

    image = Image.open(io.BytesIO(response.content))
    for_mat = image.format.lower()
    new_file_path = Path(save_file_path).with_suffix(f'.{for_mat}')
    image.save(new_file_path)
    return new_file_path


if __name__ == '__main__':
    url = 'https://hbimg.huaban.com/b0ba0c94a98fe412140021ac51cd66016866f8e8187097-qmJjTx'
    save_file_path = 'test.jpg'
    download_image(url, save_file_path)